package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.PO.InvInoutBill;
import com.ruoyi.buy.domain.entity.PO.InvInoutBillItem;
import com.ruoyi.buy.domain.entity.PO.InvInventoryInfo;
import com.ruoyi.buy.domain.entity.PO.InvWarehouseInfo;
import com.ruoyi.buy.domain.entity.inv0002.Inv0002ImportData;
import com.ruoyi.buy.domain.entity.inv0002.Inv0002TableData;
import com.ruoyi.buy.domain.entity.inv0002.Inv0002ViewData;
import com.ruoyi.buy.mapper.PO.InvInoutBillItemMapper;
import com.ruoyi.buy.mapper.PO.InvInoutBillMapper;
import com.ruoyi.buy.mapper.PO.InvInventoryInfoMapper;
import com.ruoyi.buy.mapper.PO.InvWarehouseInfoMapper;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.buy.service.IInvInventoryInfoService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-18
 */
@Service
@Transactional
public class InvInventoryInfoServiceImpl implements IInvInventoryInfoService
{
    @Autowired
    private InvInventoryInfoMapper invInventoryInfoMapper;

    @Autowired
    private InvInoutBillItemMapper invInoutBillItemMapper;

    @Autowired
    private ICommonService iCommonService;

    @Autowired
    private InvInoutBillMapper invInoutBillMapper;

    @Autowired
    private InvWarehouseInfoMapper invWarehouseInfoMapper;


    /**
     * 查询库存信息
     * 
     * @param id 库存信息主键
     * @return 库存信息
     */
    @Override
    public InvInventoryInfo selectInvInventoryInfoById(Long id)
    {
        return invInventoryInfoMapper.selectInvInventoryInfoById(id);
    }

    /**
     * 查询库存信息列表
     * 
     * @param invInventoryInfo 库存信息
     * @return 库存信息
     */
    @Override
    public List<Inv0002TableData> selectInvInventoryInfoList(InvInventoryInfo invInventoryInfo)
    {
        return invInventoryInfoMapper.selectInvInventoryInfoList(invInventoryInfo);
    }

    /**
     * 新增库存信息
     * 
     * @param invInventoryInfo 库存信息
     * @return 结果
     */
    @Override
    public int insertInvInventoryInfo(InvInventoryInfo invInventoryInfo)
    {
        invInventoryInfo.setCreateTime(DateUtils.getNowDate());
        return invInventoryInfoMapper.insertInvInventoryInfo(invInventoryInfo);
    }

    /**
     * 修改库存信息
     * 
     * @param invInventoryInfo 库存信息
     * @return 结果
     */
    @Override
    public int updateInvInventoryInfo(InvInventoryInfo invInventoryInfo)
    {
        invInventoryInfo.setUpdateTime(DateUtils.getNowDate());
        return invInventoryInfoMapper.updateInvInventoryInfo(invInventoryInfo);
    }

    /**
     * 批量删除库存信息
     * 
     * @param ids 需要删除的库存信息主键
     * @return 结果
     */
    @Override
    public int deleteInvInventoryInfoByIds(Long[] ids)
    {
        return invInventoryInfoMapper.deleteInvInventoryInfoByIds(ids);
    }

    /**
     * 删除库存信息信息
     * 
     * @param id 库存信息主键
     * @return 结果
     */
    @Override
    public int deleteInvInventoryInfoById(Long id)
    {
        return invInventoryInfoMapper.deleteInvInventoryInfoById(id);
    }

    @Override
    public List<Inv0002ViewData> getViewData(InvInventoryInfo invInventoryInfo) {
        return invInoutBillItemMapper.selectInvInoutBillItemCondition(invInventoryInfo);
    }

    @Override
    public int locationChange(List<InvInventoryInfo> invInventoryInfos) {
        return 1;
    }

    @Override
    public int importData(List<Inv0002ImportData> list) throws Exception {
        //每次导入都新建一个移动单
        InvInoutBill invInoutBill = new InvInoutBill();
        invInoutBill.setvBillNo(iCommonService.getNo("RK"));
        invInoutBill.setIotype("I");
        invInoutBill.setvType("IQC");
        invInoutBill.setvCorBillNo("期初");
        invInoutBill.setdExecute(DateUtils.getNowDate());
        invInoutBill.setCreateTime(DateUtils.getNowDate());
        invInoutBill.setCreateBy(iCommonService.getUsername());
        invInoutBillMapper.insertInvInoutBill(invInoutBill);

        //反查ID
        InvInoutBill search = new InvInoutBill();
        search.setvBillNo(invInoutBill.getvBillNo());
        List<InvInoutBill> invInoutBills = this.invInoutBillMapper.selectInvInoutBillList(search);
        Long nMainId = invInoutBills.get(0).getId();

        //处理明细
        for (Inv0002ImportData item:list
             ) {
            Long nWHId = 0L;
            Long nAreaId = 0L;
            Long nLocationId = 0L;

            InvInoutBillItem invInoutBillItem = new InvInoutBillItem();
            invInoutBillItem.setnMainId(nMainId);
            invInoutBillItem.setvSpaName(item.getvSpaName());
            invInoutBillItem.setvSpaModel(item.getvSpaModel());
            invInoutBillItem.setvSpaBrand(item.getvSpaBrand());
            invInoutBillItem.setnQty(item.getnQty());
            invInoutBillItem.setnPrice(item.getnPrice());

            //处理仓库，库区，库位ID
            InvWarehouseInfo invWarehouseInfo = new InvWarehouseInfo();
            invWarehouseInfo.setvWHName(item.getvWHName());
            List<InvWarehouseInfo> warehouseInfos = this.invWarehouseInfoMapper.selectInvWarehouseInfoList(invWarehouseInfo);
            if (warehouseInfos.isEmpty()){
                throw new Exception(item.getvWHName() + "仓库不存在，请确认");
            }
            nWHId = warehouseInfos.get(0).getId();

            //再处理库区库位联合的字段
            String areaAndLo = item.getvLocation();
            String[] groups = areaAndLo.split(" ");
            //查库区
            invWarehouseInfo = new InvWarehouseInfo();
            invWarehouseInfo.setParentId(nWHId);
            invWarehouseInfo.setvWHName(groups[0]);
            warehouseInfos = this.invWarehouseInfoMapper.selectInvWarehouseInfoList(invWarehouseInfo);
            if (warehouseInfos.isEmpty()){
                throw new Exception(groups[0] + "库区不存在，请确认");
            }
            nAreaId = warehouseInfos.get(0).getId();
            //查库位
            invWarehouseInfo = new InvWarehouseInfo();
            invWarehouseInfo.setParentId(nAreaId);
            invWarehouseInfo.setvWHName(groups[1]);
            warehouseInfos = this.invWarehouseInfoMapper.selectInvWarehouseInfoList(invWarehouseInfo);
            if (warehouseInfos.isEmpty()){
                throw new Exception(groups[1] + "库位不存在，请确认");
            }
            nLocationId = warehouseInfos.get(0).getId();
            invInoutBillItem.setvWHId(String.valueOf(nWHId));
            invInoutBillItem.setvAreaId(String.valueOf(nAreaId));
            invInoutBillItem.setvLocationId(String.valueOf(nLocationId));
            invInoutBillItem.setCreateTime(DateUtils.getNowDate());
            invInoutBillItem.setCreateBy(iCommonService.getUsername());
            this.invInoutBillItemMapper.insertInvInoutBillItem(invInoutBillItem);
            //处理库存
            InvInventoryInfo invInventoryInfo = new InvInventoryInfo();
            invInventoryInfo.setvSpaName(item.getvSpaName());
            invInventoryInfo.setvSpaModel(item.getvSpaModel());
            invInventoryInfo.setvSpaBrand(item.getvSpaBrand());
            invInventoryInfo.setnQty(item.getnQty());
            invInventoryInfo.setnPrice(item.getnPrice());
            invInventoryInfo.setCreateTime(DateUtils.getNowDate());
            invInventoryInfo.setCreateBy(iCommonService.getUsername());
            invInventoryInfo.setvWHId(String.valueOf(nWHId));
            invInventoryInfo.setvAreaId(String.valueOf(nAreaId));
            invInventoryInfo.setvLocationId(String.valueOf(nLocationId));
            invInventoryInfo.setvLotNo(1L);
            this.invInventoryInfoMapper.insertInvInventoryInfo(invInventoryInfo);
        }
        return 1;
    }
}
